<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
 <head>
  <meta http-equiv="content-type" content="text/html; charset=UTF-8">
  <title>Life cycle of an extension</title>

 </head>
 <body><div class="manualnavbar" style="text-align: center;">
 <div class="prev" style="text-align: left; float: left;"><a href="internals2.structure.globals.html">Extension globals</a></div>
 <div class="next" style="text-align: right; float: right;"><a href="internals2.structure.tests.html">Testing an extension</a></div>
 <div class="up"><a href="internals2.structure.html">扩展的结构</a></div>
 <div class="home"><a href="index.html">PHP Manual</a></div>
</div><hr /><div id="internals2.structure.lifecycle" class="sect1">
  <h2 class="title">Life cycle of an extension</h2>

  <p class="simpara">
   A PHP extension goes through several phases during its lifetime. All of
   these phases are opportunities for the developer to perform various
   initialization, termination, or informational functions. The Zend API allows
   for hooks into five separate phases of an extension&#039;s existence, apart from
   calls by PHP functions.
  </p>
  
  <div class="sect2" id="internals2.structure.lifecycle.mod-vs-req">
   <h3 class="title">Loading, unloading, and requests</h3>
   
   <p class="simpara">
    As the Zend engine runs, it processes one or more &quot;requests&quot; from its
    client. In the traditional CGI implementation, this corresponds to one
    execution of a process. However, many other implementations, most notably
    the Apache module, can map many requests onto a single PHP process. A PHP
    extension may thus see many requests in its lifetime.
   </p>
  </div>

  <div class="sect2" id="internals2.structure.lifecycle.overview">
   <h3 class="title">Overview</h3>
   
   <ul class="itemizedlist">
    <li class="listitem">
     <span class="simpara">
      In the Zend API, a module is loaded into memory only once when the
      associated PHP process starts up. Each module receives a call to the
      &quot;module initialization&quot; function specified in its
      <strong><code>zend_module</code></strong> structure as it is loaded.
     </span>
    </li>
    
    <li class="listitem">
     <span class="simpara">
      Whenever the associated PHP process starts to handle a request from its
      client - i.e. whenever the PHP interpreter is told to start working - each
      module receives a call to the &quot;request initialization&quot; function specified
      in its <strong><code>zend_module</code></strong> structure.
     </span>
    </li>

    <li class="listitem">
     <span class="simpara">
      Whenever the associated PHP process is done handling a request, each
      module receives a call to the &quot;request termination&quot; function specified in
      its <strong><code>zend_module</code></strong> structure.
     </span>
    </li>
    
    <li class="listitem">
     <span class="simpara">
      A given module is unloaded from memory when its associated PHP process is
      shut down in an orderly manner. The module receives a call to the &quot;module
      termination&quot; function specified in its
      <strong><code>zend_module</code></strong> structure at this time.
     </span>
    </li>
   </ul>

  </div>

  <div class="sect2" id="internals2.structure.lifecycle.what-when">
   <h3 class="title">What to do, and when to do it</h3>

   <p class="simpara">
    There are many tasks that might be performed at any of these four points.
    This table details where many common initialization and termination tasks
    belong.
   </p>
   
   <table class="doctable table">
    <caption><strong>What to do, and when to do it</strong></caption>
    
     <thead>
      <tr>
       <th>Module initialization/termination</th>
       <th>Request initialization/termination</th>
      </tr>

     </thead>

     <tbody class="tbody">
      <tr>
       <td>Allocate/deallocate and initialize module global variables</td>
       <td>
        Allocate/deallocate and initialize request-specific variables
       </td>
      </tr>

      <tr>
       <td>Register/unregister class entries</td>
       <td class="empty">&nbsp;</td>
      </tr>

      <tr>
       <td>Register/unregister INI entries</td>
       <td class="empty">&nbsp;</td>
      </tr>

      <tr>
       <td>Register constants</td>
       <td class="empty">&nbsp;</td>
      </tr>

     </tbody>
    
   </table>


  </div>

  <div class="sect2" id="internals2.structure.lifecycle.info">
   <h3 class="title">The <span class="function"><a href="function.phpinfo.html" class="function">phpinfo()</a></span> callback</h3>
   
   <p class="simpara">
    Aside from globals initialization and certain rarely-used callbacks, there
    is one more part of a module&#039;s lifecycle to examine: A call to
    <span class="function"><a href="function.phpinfo.html" class="function">phpinfo()</a></span>. The output a user sees from this call, whether
    text or HTML or anything else, is generated by each individual extension
    that is loaded into the PHP interpreter at the time the call is made.
   </p>
   
   <p class="simpara">
    To provide for format-neutral output, the header
    &quot;ext/standard/info.h&quot; provides an array of functions to produce
    standardized display elements. Specifically, several functions which create
    the familiar tables exist:
   </p>
   
   <dl>

    
     <dt>
<span class="function"><strong>php_info_print_table_start()</strong></span></dt>

     <dd>

      <span class="simpara">
       Open a table in <span class="function"><a href="function.phpinfo.html" class="function">phpinfo()</a></span> output. Takes no parameters.
      </span>
     </dd>

    
    
     <dt>
<span class="function"><strong>php_info_print_table_header()</strong></span></dt>

     <dd>

      <span class="simpara">
       Print a table header in <span class="function"><a href="function.phpinfo.html" class="function">phpinfo()</a></span> output. Takes one
       parameter, the number of columns, plus the same number of
       <span class="type"><span class="type char *">char *</span></span> parameters which are the texts for each column
       heading.
      </span>
     </dd>

    
    
     <dt>
<span class="function"><strong>php_info_print_table_row()</strong></span></dt>

     <dd>

      <span class="simpara">
       Print a table row in <span class="function"><a href="function.phpinfo.html" class="function">phpinfo()</a></span> output. Takes one
       parameter, the number of columns, plus the same number of
       <span class="type"><span class="type char *">char *</span></span> parameters which are the texts for each column
       content.
      </span>
     </dd>

    
    
     <dt>
<span class="function"><strong>php_info_print_table_end()</strong></span></dt>

     <dd>

      <span class="simpara">
       Close a table formerly opened by
       <span class="function"><strong>php_info_print_table_start()</strong></span>. Takes no parameters.
      </span>
     </dd>

    
   </dl>

   
   <p class="simpara">
    Using these four functions, it is possible to produce status information for
    nearly any combination of features in an extension. Here is the information
    callback from the counter extension:
   </p>

   <div class="example" id="internals2.structure.lifecycle.info.counter">
    <p><strong>Example #1 counter&#039;s PHP_MINFO function</strong></p>
    <div class="example-contents">
<div class="ccode"><pre class="ccode">/* {{{ PHP_MINFO(counter) */
PHP_MINFO_FUNCTION(counter)
{
    char        buf[10];

    php_info_print_table_start();
    php_info_print_table_row(2, &quot;counter support&quot;, &quot;enabled&quot;);
    snprintf(buf, sizeof(buf), &quot;%ld&quot;, COUNTER_G(basic_counter_value));
    php_info_print_table_row(2, &quot;Basic counter value&quot;, buf);
    php_info_print_table_end();
}
/* }}} */</pre>
</div>
    </div>

   </div>
  </div>

 </div><hr /><div class="manualnavbar" style="text-align: center;">
 <div class="prev" style="text-align: left; float: left;"><a href="internals2.structure.globals.html">Extension globals</a></div>
 <div class="next" style="text-align: right; float: right;"><a href="internals2.structure.tests.html">Testing an extension</a></div>
 <div class="up"><a href="internals2.structure.html">扩展的结构</a></div>
 <div class="home"><a href="index.html">PHP Manual</a></div>
</div></body></html>
